<!-- 审核表单 -->
<template>
  <div class="audit-form-wp">
  	<div class="h-full" style="overflow:auto;">
  		<el-card class="mb-15 preview-card">
	    	<template #header>
	    		<div class="flex justify-between items-center">
	    			<div class="title">{{_d.oForm.formName || ''}}</div>
	    			<div>
	    				<el-button size="small" @click="exportContent()">导出</el-button>
	    				<el-button size="small" @click="printContent()">打印</el-button>
	    			</div>
	    		</div>
	    	</template>
	    	<pdfPreview v-if="_d.oForm.content" ref="previewRef" :is-init="false" :config="_d.oForm.content"></pdfPreview>
	    </el-card>
	    <el-row :gutter="15">
	    	<el-col :span="12">
	    		<div class="file-box">
				    <el-card class="mb-15">
				    	<template #header>
			    		<div class="flex justify-between items-center">
			    			<div class="title">文件清单</div>
			    			<div>
			    				<el-button v-if="canEdit" size="small" @click="uploadFile()">上传</el-button>
			    			</div>
			    		</div>
			    	</template>
			    		<el-table :data="_d.fileList" style="width: 100%" border height="200px">
								<el-table-column type="index" label="序号" width="55" align="center" />
								<el-table-column prop="oldName" label="文件名称" show-overflow-tooltip />
								<!-- <el-table-column prop="state" label="提交结果" width="200" show-overflow-tooltip /> -->
								<el-table-column label="操作" width="150" align="center" fixed="right" show-overflow-tooltip>
									<template #default="scope">
										<SeeFile :name="scope.row.oldName" :url="scope.row.url"/>
									</template>
								</el-table-column>
							</el-table>
				    </el-card>
			    </div>
	    	</el-col>
	    	<el-col :span="12">
	    		<div class="file-box">
				    <el-card class="mb-15">
				    	<template #header>
			    		<div class="flex justify-between items-center">
			    			<div class="title">关联文件</div>
			    		</div>
			    	</template>
			    		<el-table :data="_d.relationList" style="width: 100%" border height="200px">
								<el-table-column type="index" label="序号" width="55" align="center" />
								<el-table-column prop="showName" label="文件名称" show-overflow-tooltip />
								<el-table-column label="操作" width="150" align="center" fixed="right" show-overflow-tooltip>
									<template #default="scope">
										<SeeFile :name="scope.row.showName" :url="scope.row.url"/>
									</template>
								</el-table-column>
							</el-table>
				    </el-card>
			    </div>
	    	</el-col>
	    </el-row>
		  <div class="content-box">
		  	<el-card class="mb-15">
		  		<div v-for="(item, index) in _d.contentList" :key="index" class="form-item">
		  			<div class="label">{{item.label}}</div>
		  			<div class="my-5">
		  				<el-input v-model="item.content" type="textarea" :rows="4" placeholder="请输入"/>
		  			</div>
		  			<div class="text-right">
		  				<span>时间：</span>
		  				<el-date-picker
			          v-model="item.time"
			          type="date"
			          value-format="YYYY-MM-DD"
			          placeholder="请选择"/>
		  			</div>
		  		</div>
		  	</el-card>
		  </div>
		  <div v-if="_d.oForm.checkResult != 2" class="py-10 text-center" style="background:#fff;">
		  	<el-button size="medium" @click="setAudit(0)">驳回</el-button>
		  	<el-button size="medium" type="primary" @click="setAudit(2)">通过</el-button>
		  </div>
  	</div>
  </div>
</template>

<script setup>
	import { reactive, ref, onMounted } from 'vue'
	import {useRouter} from "vue-router";
	import { ElNotification, ElMessageBox } from 'element-plus';
	import {$get, $post, $postFile} from '/@/utils/request';
	import pdfPreview from "/@/views/template/pdf/preview/index.vue";
	import {useUserInfo} from "/@/stores/userInfo.ts";
	import SeeFile from "/@/components/SeeFile/index.vue";

	const router = useRouter();
	const userInfo = useUserInfo();

	const _d = reactive({
		oForm: {},
		fileList: [],
		relationList: [],
		contentList: [
			{
				label: '合同承办科室负责人审核意见：',
				content: '',
				time: ''
			},
			{
				label: '业务分管领导审核意见：',
				content: '',
				time: ''
			},
			{
				label: '财务分管领导审核意见：',
				content: '',
				time: ''
			},
			{
				label: '主要领导审核意见：',
				content: '',
				time: ''
			},
			{
				label: '法律顾问审核意见：',
				content: '',
				time: ''
			}
		]
	})

	onMounted(() => {
		let formId = localStorage.getItem('contract_audit_form_id');
		if(formId) {
			getFormContent(formId);
			getRecord(formId);
		}
	})

	// 查询当前表单的内容
	function getFormContent(id) {
		// 查询表单内容
		$post('/projectService/geformbyid/'+id).then(res => {
			try {
				res.result.content = JSON.parse(res.result.content);
				_d.oForm = res.result;
			} catch(err) {
				console.log('解析content失败');
				_d.oForm = {};
			} 
		})
		// 查询当前表单的附件列表
		getFileList(id);
		// 查询表单关联文档列表
		getRelationList(id);
	}

	// 查询当前表单的附件列表
	function getFileList(id) {
		$get('/projectService/getformfiles/'+id).then(res => {
			_d.fileList = res.result.map(item => {
				item.oldName = item.fileName+item.suffix;
				return item;
			});
		})
	}

	// 查询表单关联文档列表
	function getRelationList(id) {
		$get('/projectService/getformtemplatefiles/'+id).then(res => {
			_d.relationList = res.result.map(item => {
				item.showName = item.fileName+item.suffix;
				return item;
			});
		})
	}

	// 获取审核历史
	function getRecord(id) {
		$get('/projectService/getoneauditrecord/'+id).then(res => {
			if(res?.result?.auditContent) {
				_d.contentList = JSON.parse(res.result.auditContent);
			}
		})
	}

	// 审核
	function setAudit(status) {
		let text = status == 2 ? '通过' : '驳回';
		ElMessageBox.confirm(`此操作将${text}该表单，是否继续？`, '提示', {
			type: 'warning'
		}).then(async () => {
			// 添加审核记录
			if(status == 2) {
				await insauditrecord();
			}
			// 确认审核
			$get(`/projectService/auditforms/${_d.oForm.id}/${status}`).then(res => {
				if(res.code == 200) {
					ElNotification({
				    title: '提示',
				    message: '保存成功',
				    type: 'success',
				  })
					router.back();
				}
			})
		})
	}

	// 添加审核记录
	async function insauditrecord() {
		let msg = '';
		for(let i = 0; i < _d.contentList.length; i++) {
			let item = _d.contentList[i];
			if(!item.content) {
				msg = item.label+'不能为空';
				break;
			} else if(!item.time) {
				msg = item.label+'时间不能为空';
				break;
			}
		}
		if(msg) {
			ElNotification.error({
				title: '提示',
				message: msg
			})
			throw msg;
		}
		let params = {
			auditContent: JSON.stringify(_d.contentList),
			contractAuditor: userInfo.$state.userInfos.userId,
			projectId: _d.oForm.projectId,
			formId: _d.oForm.id
		}
		$post('/projectService/insauditrecord', params)
	}

	// 导出内容
	function exportContent() {
		previewRef.value.export();
	}

	// 打印
	function printContent() {
		previewRef.value.print();
	}

	// 删除
	function deleteFile(row) {
		ElMessageBox.confirm('此操作将删除该文件，是否继续？', '提示', {
			type: 'warning'
		}).then(() => {
			$post('/projectService/removeformfile/'+row.id).then(res => {
				if(res.code == 200) {
					// 查询当前表单的附件列表
					getFileList(_d.formId);
				}
			})
		})
	}

</script>

<style lang="scss" scoped>
.audit-form-wp {
	.preview-card {
		.el-card__header {
			padding: 10px 20px;
		}
	}
	.content-box {

	}
}
</style>